package cleancode.model;

/**
 * Model for users.
 */
public class User extends NameBasedModel
{
    private NameOrientedModelSet<Role> roles = new NameOrientedModelSet<>();

    public User( String name )
    {
        super( name );
    }

    public boolean checkPermission( Operation operation )
    {
        if( operation.isAwaysAllowed() )
        {
            return true;
        }
        for( Role role : roles )
        {
            if( role.supportOperation( operation ) )
            {
                return true;
            }
        }
        return false;
    }

    public NameOrientedModelSet<Role> getRoles()
    {
        return roles;
    }

    public NameOrientedModelSet<Operation> getSupportedOperations()
    {
        NameOrientedModelSet<Operation> operations = new NameOrientedModelSet<>();
        for( Role role : roles )
        {
            operations.addAll( role.getSupportedOperations() );
        }
        return operations;
    }

}
